##
#                  ,           /)   
#   _   _ __  _/_   __    _  _(/  _ 
#  (___(/_/ (_(___(_/_)__(/_(_(__(/_
#                .-/                
#               (_/     
#
#  http://code.google.com/p/centipede/
#  Understanding & simplicity
#  Copyright 2009, Asbjorn Enge. All rights reserved.
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
#  MA  02111-1307  USA
#
#  (C) 2010
#
##
#
#  The expose decorator is inspired by the bespin project.
#  http://mozillalabs.com/bespin/
#
##
#
#  Framework
#
##

from urlrelay import url, URLRelay
from webob import Request, Response

_template_path='templates'
_static_path='statics'

## Expose urls
#

def expose(url_pattern, method=None, content_type='text/html', charset='UTF-8', auth=True, skip_token_check=False, profile=False):
    """Expose this function to the world, matching the given URL pattern
    and, optionally, HTTP method."""
    def entangle(func):
        @url(url_pattern, method)
        def wrapped(environ, start_response):

            request = Request(environ)
            response = Response()
                
            start_response('200 OK', [('Content-type', '%s; charset=%s' % (content_type, charset))])

            return [func(request, response)]

    return entangle

## Setup templates
#

from mako.template import Template
from mako.lookup import TemplateLookup

#TODO: Mako templates can also be cached :)

mylookup = TemplateLookup(directories=[_template_path], output_encoding='utf-8')

def template(templatename, **kwargs):
    mytemplate = mylookup.get_template(templatename)
    return mytemplate.render(**kwargs)


